package com.cn.hospital.controller.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cn.hospital.entity.SysUser;
import com.cn.hospital.service.auth.SysUserService;
import com.cn.hospital.message.Msg;
import com.cn.hospital.message.PageResponse;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class SysUserController {
    @Reference
    SysUserService sysUserService;

    @RequestMapping("/byId")
    private Msg findById(int id) {
        SysUser sysUser = sysUserService.findById(id);
        return new Msg<>().ok(200, sysUser);
    }

    @RequestMapping("/page")
    public Msg getById(@RequestBody PageResponse pageResponse) {

        IPage<SysUser> page = new Page<>(pageResponse.getPage(), pageResponse.getSize());
        IPage<SysUser> pageResult = sysUserService.page(page);

        Map<String,Object> map = new HashMap<>();
        map.put("total", pageResult.getTotal());
        map.put("page", pageResult.getPages());
        List<SysUser> sysUsers = pageResult.getRecords();
        if (sysUsers != null && sysUsers.size() > 0) {
            sysUsers.forEach(sysUser -> {
                sysUser.setPassword("");
            });
        }
        map.put("data", sysUsers);
        return new Msg<>().ok(200, map);
    }
}
